/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DomainEditForm.java
 *
 * Created on 2009-1-18, 14:13:00
 */

package com.guzzservices.easydomain.ui.frame;

import com.guzzservices.easydomain.logic.DomainManager;
import com.guzzservices.easydomain.logic.domain.TopDomain;
import com.guzzservices.easydomain.logic.thirdpart.handler.IRecordAManager;
import com.guzzservices.easydomain.logic.thirdpart.handler.RecordAHandlerFactory;
import com.guzzservices.easydomain.util.ComponentCenter;
import javax.swing.DefaultComboBoxModel;
import org.jdesktop.application.Action;
import org.jdesktop.application.Task;

/**
 *
 * @author Administrator
 */
public class DomainEditForm extends javax.swing.JDialog {

    /** Creates new form DomainEditForm
     * @deprecated 仅供NetBean设计界面使用，程序不可以调用！
     */
    public DomainEditForm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        initValues() ;
    }
    
    public DomainEditForm(java.awt.Frame parent, boolean modal, TopDomain domain) {
        super(parent, modal);
        this.domain = domain ;
        this.isAdd = (this.domain == null) ;
        initComponents();
        initValues() ;
    }

    private void initValues(){
        if(this.domain != null){
            this.domainPrefixText.setText(domain.getDomainPrefix());
            this.domainSuffixBox.setSelectedItem(domain.getDomainSuffix());
            this.domainPasswordText.setText(domain.getPassword());
            this.domainRegisterBox.setSelectedItem(domain.getProvider()) ;
        }

        if(!this.isAdd){ //只允许修改密码和域名提供商
            this.domainPrefixText.setEditable(false) ;
            this.domainSuffixBox.setEditable(false) ;
            this.domainSuffixBox.setEnabled(false) ;
        }

        IRecordAManager ram =  RecordAHandlerFactory.getRecordAManager() ;
        
        this.domainRegisterBox.setModel(new DefaultComboBoxModel(ram.getAllProviderNames())) ;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        domainPrefixText = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        domainSuffixBox = new javax.swing.JComboBox();
        domainPasswordText = new javax.swing.JPasswordField();
        jLabel7 = new javax.swing.JLabel();
        domainRegisterBox = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.guzzservices.easydomain.ui.frame.DDSProjectApp.class).getContext().getResourceMap(DomainEditForm.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setLocationByPlatform(true);
        setName("Form"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        domainPrefixText.setText(resourceMap.getString("domainPrefixText.text")); // NOI18N
        domainPrefixText.setName("domainPrefixText"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        domainSuffixBox.setMaximumRowCount(10);
        domainSuffixBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "com", "net", "org", "cn", "com.cn", "net.cn", "org.cn" }));
        domainSuffixBox.setName("domainSuffixBox"); // NOI18N

        domainPasswordText.setText(resourceMap.getString("domainPasswordText.text")); // NOI18N
        domainPasswordText.setName("domainPasswordText"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        domainRegisterBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "新网", "万网" }));
        domainRegisterBox.setName("domainRegisterBox"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(com.guzzservices.easydomain.ui.frame.DDSProjectApp.class).getContext().getActionMap(DomainEditForm.class, this);
        jButton1.setAction(actionMap.get("saveTopDomainForm")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("cacelEdit")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 454, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(domainRegisterBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(domainPasswordText, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(domainPrefixText, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(domainSuffixBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(domainPrefixText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(domainSuffixBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(domainPasswordText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(domainRegisterBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {domainPasswordText, jLabel4, jLabel7});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {domainPrefixText, domainSuffixBox, jLabel3, jLabel6});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {domainRegisterBox, jLabel5, jLabel8});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DomainEditForm dialog = new DomainEditForm(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void cacelEdit() {
        this.dispose();
    }

    @Action
    public Task saveTopDomainForm() {
        return new SaveTopDomainFormTask(org.jdesktop.application.Application.getInstance(com.guzzservices.easydomain.ui.frame.DDSProjectApp.class));
    }

    private class SaveTopDomainFormTask extends org.jdesktop.application.Task<Object, Void> {
        private String oldDomainFull ;

        SaveTopDomainFormTask(org.jdesktop.application.Application app) {
            // Runs on the EDT.  Copy GUI state that
            // doInBackground() depends on from parameters
            // to SaveTopDomainFormTask fields, here.
            super(app);
            DomainEditForm out = DomainEditForm.this ;
            out.setVisible(false) ;

            if(!out.isAdd){
                this.oldDomainFull = out.domain.getFullDomain() ;
            }else{
                out.domain = new TopDomain() ;
            }

            out.domain.setDomainPrefix(out.domainPrefixText.getText()) ;
            out.domain.setDomainSuffix((String) out.domainSuffixBox.getSelectedItem());
            out.domain.setPassword(new String(out.domainPasswordText.getPassword()));
            out.domain.setProvider((String) out.domainRegisterBox.getSelectedItem());
        }
        @Override protected Object doInBackground() {
            DomainManager dm = DomainManager.getDomainManager() ;
            DomainEditForm out = DomainEditForm.this ;

            ComponentCenter.getInstance().setTextToStatusPanel("正在保存主域名：" + out.domain.getFullDomain());

            try{
                if(out.isAdd){
                    dm.addTopDomain(out.domain) ;
                }else{
                    dm.updateTopDomain(domain);
                }

                dm.refreshARcordsFromServer(domain) ;

            }catch(Exception e){
                ComponentCenter.getInstance().setTextToStatusPanel("主域名：" + DomainEditForm.this.domain.getFullDomain() + " 保存失败：" + e.getMessage());
            }

            return null;  // return your result
        }
        @Override protected void succeeded(Object result) {
           ComponentCenter.getInstance().setTextToStatusPanel("主域名：" + DomainEditForm.this.domain.getFullDomain() + " 成功保存");
           DomainEditForm.this.dispose();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPasswordField domainPasswordText;
    private javax.swing.JTextField domainPrefixText;
    private javax.swing.JComboBox domainRegisterBox;
    private javax.swing.JComboBox domainSuffixBox;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    // End of variables declaration//GEN-END:variables

    private TopDomain domain ;
    private boolean isAdd ;
}
